<html>

<head>

</head>

<body>

    昵称:<input type="text" name="username" id="username"><span id="info"></span>
    <script>
        var username = document.getElementById("username");
        username.onfocus = function () {
            document.getElementById("info").innerHTML = "请输入6-12位的昵称";
        }

        username.onblur = function () {
            var reg = /^\w{6,12}$/
            var flag = reg.test(username.value);
            if (flag == true) {
                document.getElementById("info").innerHTML = "合法昵称";
            } else {
                document.getElementById("info").innerHTML = "昵称不合法";
            }
        }

    </script>

</body>

</html>